//
//  UIBaseRefreshView.m
//  Qianbao
//
//  Created by zhangtie on 13-8-28.
//  Copyright (c) 2013年 qianwang365. All rights reserved.
//

#import "UIBaseRefreshView.h"
@implementation QBCellSelectedState

@end
@implementation UIBaseRefreshView

@synthesize mainList = _mainList;

- (void)dealloc
{
    [_headRefreshView.layer removeAllAnimations];
    [_headRefreshView removeFromSuperview];
    RELEASE_SAFELY(_headRefreshView);
    
    _footRefreshView.delegate = nil;
    [_footRefreshView detach];
    RELEASE_SAFELY(_footRefreshView);
    RELEASE_SAFELY(_mainList);
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // Initialization code
        if(_headRefreshView)
        {
            [_headRefreshView removeFromSuperview];
            RELEASE_SAFELY(_headRefreshView);
        }
        self.mainList.delegate = self;
        [self.mainList addSubview:self.headRefreshView];
    }
    return self;
}

- (void)loadMore
{
    SEL callBack = @selector(loadMore);
    if(_delegate && [_delegate respondsToSelector:callBack])
    {
        [_delegate performSelector:callBack];
    }
}

- (void)refreshData
{
    SEL callBack = @selector(refreshData);
    if(_delegate && [_delegate respondsToSelector:callBack])
    {
        [_delegate performSelector:callBack];
    }
}

- (void)refreshCellDidTriggerLoading:(RefreshCell*)view
{
    [self loadMore];
}

- (void)slimeRefreshStartRefresh:(SRRefreshView*)refreshView
{
    [self refreshData];
}

- (BOOL)refreshCellDataSourceIsLoading:(RefreshCell*)view
{
    return _footRefreshView.state == RefreshLoading;
}

- (void)showHeader:(BOOL)bshow
{
    if(bshow)
    {
        if(!_headRefreshView)
        {
            [self headRefreshView];

        }
        [self.mainList addSubview:self.headRefreshView];
    }
    else
    {
        [_headRefreshView removeFromSuperview];
        RELEASE_SAFELY(_headRefreshView);
    }
}

- (void)showFooter:(BOOL)bshow
{
    if(bshow)
    {
        [self footRefreshView];
    }
    else
    {
        [_footRefreshView removeFromSuperview];
        RELEASE_SAFELY(_footRefreshView);
    }
}

- (void)endFooterLoadState
{
    if(_footRefreshView)
    {
        [_footRefreshView refreshScrollViewDataSourceDidFinishedLoading:self.mainList succeed:YES animal:NO];
    }
}

- (void)endHeaderRefreshState
{
    if(_headRefreshView)
    {
        [_headRefreshView endRefresh];
    }
}
#if TEST_PUSH_VC
static BOOL testPushVC;
#endif
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [_headRefreshView scrollViewDidScroll];
    [_footRefreshView refreshScrollViewDidScroll:scrollView];
#if TEST_PUSH_VC
    int a = scrollView.panGestureRecognizer.numberOfTouches;
    if( a ==2 )
    {
        testPushVC = YES;
    }
    else
    {
        testPushVC = NO;
    }
   
#endif
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    [_headRefreshView scrollViewDidEndDraging];
    [_footRefreshView refreshScrollViewDidEndDragging:scrollView];
#if TEST_PUSH_VC
    if([_delegate respondsToSelector:@selector(testEgg)])
    {
        [_delegate testEgg];
    }
#endif

}


- (RefreshCell *)footRefreshView
{
    if(!_footRefreshView)
    {
        _footRefreshView = [[RefreshCell attachRefreshCellTo:self.mainList
                                                    delegate:self
                                              arrowImageName:@"refreshcell_img_arrow.png"
                                                   textColor:[UIColor darkGrayColor]
                                              indicatorStyle:UIActivityIndicatorViewStyleGray
                                                        type:RefreshTypeLoadMore] retain];
    }
    return _footRefreshView;
}

- (SRRefreshView*)headRefreshView
{
    if(!_headRefreshView)
    {
        _headRefreshView = [[SRRefreshView alloc] init];
        _headRefreshView.delegate = self;
        _headRefreshView.upInset = 0;
        _headRefreshView.slimeMissWhenGoingBack = YES;
        _headRefreshView.slime.bodyColor = kPushRefreshBodyColor;
        _headRefreshView.slime.skinColor = kPushRefreshSkinColor;
        _headRefreshView.slime.lineWith = 1;
    }
    
    return _headRefreshView;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
